import cv2
import base64
import numpy as np
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

#设置chrome浏览器无界面模式
chrome_options=Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)


#获取验证码偏移量
def get_offset_position(a, b,username):
    # base64解码(解成二进制串)
    decode_jpg1 = base64.b64decode(a)
    decode_jpg2 = base64.b64decode(b)
    # 写入png文件
    with open(r'E:\UIpath Project\BaiSTH\%s\target.png'%username, 'wb') as f1:
        f1.write(decode_jpg1)
    with open(r'E:\UIpath Project\BaiSTH\%s\template.png' % username, 'wb') as f2:
        f2.write(decode_jpg2)

    otemp = r'E:\UIpath Project\BaiSTH\%s\template.png'%username
    oblk = r'E:\UIpath Project\BaiSTH\%s\target.png'%username
    target = cv2.imread(otemp, 0)
    template = cv2.imread(oblk, 0)
    w, h = target.shape[::-1]
    temp = r'E:\UIpath Project\BaiSTH\%s\temp.jpg'%username
    targ = r'E:\UIpath Project\BaiSTH\%s\targ.jpg'%username
    cv2.imwrite(temp, template)
    cv2.imwrite(targ, target)
    result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)
    x, y = np.unravel_index(result.argmax(), result.shape)
    # 展示圈出来的区域
    cv2.rectangle(template, (y, x), (y + w, x + h), (7, 249, 151), 2)  # 7, 249, 151
    return y

#滑动验证码
def Slide_check(driver,username):
    sleep(3)
    a = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/div[2]/img[1]").get_property('src').split(',')[-1]
    b = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/div[2]/img[2]").get_property('src').split(',')[-1]

    c = get_offset_position(a, b,username)  # 测出偏移量

    sleep(1)
    div = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[4]/div[2]')
    ActionChains(driver).drag_and_drop_by_offset(div, xoffset=c, yoffset=0).perform()

def user_login(username,password):
    driver.get("https://icrm.800best.com/#/login")
    sleep(1)
    driver.find_element_by_xpath('//*[@id="root"]/section/div/div[2]/form/div[1]/div/div/span/div/span/span[1]').click()
    sleep(1)
    driver.find_element_by_xpath("//*[@id='app']/div[2]/div[1]/div[2]/div/div[1]/img").click()
    sleep(0.5)
    driver.find_element_by_xpath("//*[@id='app']/div[2]/div[1]/div[2]/div/div[2]/form/div[1]/div/div/input").send_keys(username)
    sleep(0.5)
    driver.find_element_by_xpath("//*[@id='app']/div[2]/div[1]/div[2]/div/div[2]/form/div[2]/div/div/input").send_keys(password)
    sleep(0.5)
    driver.find_element_by_xpath("//*[@id='app']/div[2]/div[1]/div[2]/div/div[2]/form/button").click()

    for i in range(10):
        try:
            sleep(3)
            Slide_check(driver,username)
            if driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]")is not None: #如果还有验证码，重试
                continue
        except Exception:
            break

    try:
        driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[2]/div/div[2]/form/div[1]')
        return "密码错误"

    except Exception as e:
        sitel_list=[]
        sleep(1)
        site_count = len(driver.find_element_by_xpath('//*[@id="container_demo"]/div/div/div/ul/div[2]/div/div/div[2]/div/div/div/span/div/table/tbody').find_elements_by_tag_name("tr"))
        for site in range(1, site_count + 1):
            site_name_xpath = '//*[@id="container_demo"]/div/div/div/ul/div[2]/div/div/div[2]/div/div/div/span/div/table/tbody/tr[%s]/td[1]/div/span/span[1]' % site
            site_name = driver.find_element_by_xpath(site_name_xpath).text
            sitel_list.append(site_name)

        sleep(0.5)
        driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/a").click()

        return sitel_list

